package guilayer;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.sql.SQLException;

import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controllayer.SalesOrderCtr;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;

import modellayer.SalesOrder;

import javax.swing.JScrollPane;

public class ViewOrderPanel extends JPanel {
	private JPanel viewOrderpanel;
	
	private SalesOrderCtr sOCtr;
	private JTextField txtOrderID;
	private JTextField txtDate;
	private JButton btnViewInvoice;
	private JTextField txtDeliveryStatus;
	private JTextField txtDeliveryDate;
	private JTextField txtCustomer;
	private JLabel lblDeliveryStatus;
	private JLabel lblDeliveryDate;
	private JLabel lblCustomer;
	private SalesOrder sO;
	private JPanel productsBoughtPanel;
	private JTextField txtTotalPrice;
	private JScrollPane scrollPane;
	private JLabel lblNewLabel;
	private JTable pOTable;
	

	/**
	 * Create the panel.
	 * @throws SQLException 
	 */
	public ViewOrderPanel(int salesOrderID) throws SQLException {
		sOCtr = new SalesOrderCtr();
		sO = sOCtr.findSalesOrder(salesOrderID);
		initPanel();
		initComponents(salesOrderID);
		System.out.println(salesOrderID);
		
	}


	private void initPanel() {
		setLayout(new BorderLayout(0, 0));
		
		viewOrderpanel = new JPanel();
		viewOrderpanel.setBorder(new TitledBorder(null, "View order", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		add(viewOrderpanel, BorderLayout.CENTER);
		viewOrderpanel.setLayout(null);
		
		
	}

	private void initComponents(int salesOrderID) throws SQLException {
		JLabel lblOrderId = new JLabel("Order ID:");
		lblOrderId.setBounds(10, 27, 110, 14);
		viewOrderpanel.add(lblOrderId);
		
		txtOrderID = new JTextField(Integer.toString(sO.getSalesOrderID()));
		txtOrderID.setBounds(130, 24, 214, 20);
		txtOrderID.setEditable(false);
		viewOrderpanel.add(txtOrderID);
		txtOrderID.setColumns(10);
		
		JLabel lblDate = new JLabel("Date:");
		lblDate.setBounds(10, 58, 110, 14);
		viewOrderpanel.add(lblDate);
		
		txtDate = new JTextField(sO.getDate());
		txtDate.setColumns(10);
		txtDate.setBounds(130, 55, 214, 20);
		txtDate.setEditable(false);
		viewOrderpanel.add(txtDate);
		
		txtDeliveryStatus = new JTextField(sO.getDeliveryStatus());
		txtDeliveryStatus.setEditable(false);
		txtDeliveryStatus.setColumns(10);
		txtDeliveryStatus.setBounds(130, 86, 214, 20);
		viewOrderpanel.add(txtDeliveryStatus);
		
		txtDeliveryDate = new JTextField(sO.getDeliveryDate());
		txtDeliveryDate.setEditable(false);
		txtDeliveryDate.setColumns(10);
		txtDeliveryDate.setBounds(130, 117, 214, 20);
		viewOrderpanel.add(txtDeliveryDate);
		
		if(sO.getCustomer() != null) {
		txtCustomer = new JTextField(sO.getCustomer().getName());
		}else {
			txtCustomer = new JTextField();
		}
		txtCustomer.setEditable(false);
		txtCustomer.setColumns(10);
		txtCustomer.setBounds(130, 148, 214, 20);
		viewOrderpanel.add(txtCustomer);
		
		lblDeliveryStatus = new JLabel("Delivery status:");
		lblDeliveryStatus.setBounds(10, 89, 110, 14);
		viewOrderpanel.add(lblDeliveryStatus);
		
		lblDeliveryDate = new JLabel("Delivery date:");
		lblDeliveryDate.setBounds(10, 120, 110, 14);
		viewOrderpanel.add(lblDeliveryDate);
		
		lblCustomer = new JLabel("Customer:");
		lblCustomer.setBounds(10, 151, 110, 14);
		viewOrderpanel.add(lblCustomer);
		
		btnViewInvoice = new JButton("View invoice");
		btnViewInvoice.setBounds(234, 398, 110, 23);
		viewOrderpanel.add(btnViewInvoice);
		
		initProductsBought();
		
		
	}

	private void initProductsBought() {
		productsBoughtPanel = new JPanel();
		productsBoughtPanel.setBorder(new TitledBorder(null, "Products bought", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		productsBoughtPanel.setBounds(10, 179, 334, 208);
		viewOrderpanel.add(productsBoughtPanel);
		productsBoughtPanel.setLayout(null);
		
		txtTotalPrice = new JTextField();
		txtTotalPrice.setBounds(238, 177, 86, 20);
		productsBoughtPanel.add(txtTotalPrice);
		txtTotalPrice.setColumns(10);
		
		String[][] data = sOCtr.partOrderToTableArray(sO.getSalesOrderID());
		String[] column = {"Product", "Amount", "price"};
		
		pOTable = new JTable(data, column){
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {                
	                return false;               
	        }
	    };	
	
		pOTable.getTableHeader().setReorderingAllowed(false);
		pOTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		pOTable.setFillsViewportHeight(true);
		pOTable.setAutoCreateRowSorter(true);
		pOTable.setVisible(true);	
		scrollPane = new JScrollPane(pOTable);
		scrollPane.setBounds(10, 21, 314, 148);
		productsBoughtPanel.add(scrollPane);
		
		lblNewLabel = new JLabel("Total price:");
		lblNewLabel.setBounds(156, 180, 72, 14);
		productsBoughtPanel.add(lblNewLabel);
	}
}
